package com.cheer.ba.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cheer.ba.R;
import com.cheer.ba.choosepic.ui.MainActivity;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.CommentAdp;
import com.cheer.ba.model.CommentModel;
import com.cheer.ba.presenter.impl.CommentPresenter;
import com.cheer.ba.utils.DensityUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.animation.CustomAnimation;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.ShowPopupView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 用户评价列表
 * Created by Hq on 2017/7/4.
 */
public class CommentListAty extends BaseCommAty implements ShowPopupView.OnItemPopupClick {

    @BindView(R.id.id_comment_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_comment_recyclev)
    RecyclerView mRecycleView;
    private CommentAdp mCommentAdp;
    //存储柴油信息
    private List<CommentModel> mComments = new ArrayList<CommentModel>();

    //弹出头像框
    private ShowPopupView popupView;
    public static final String[] POPUP_ITEMS = {"删除"};
    public static final int[] POPUP_ITEMS_COLOR = {R.color.red};

    //记录删除评论或者回复的下标
    private int mRecordItemPos;
    //记录删除的评论或者回复的id
    private String mRecordItemId;

    private String mUid;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_comment_list, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CommentPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mComments.isEmpty();
    }

    @Override
    public void initView() {
        popupView = new ShowPopupView(aty);
        popupView.setItemColor(POPUP_ITEMS_COLOR);
        popupView.setOnItemPopupClick(this);

        mUid = getIntent().getStringExtra(IntentKey.General.KEY_ID);
        setTitleText("评论列表");
//        setTitleRight("评论", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showActivity(aty, MainActivity.class);
//            }
//        });

        int width = DensityUtils.getScreenW(aty);
        setXRefreshView(mXRefreshView, true, true, false, false, new SmileyHeaderView(aty), new CustomerFooter(aty));
        mRecycleView.setLayoutManager(new LinearLayoutManager(aty));
        mRecycleView.setHasFixedSize(true);
        mCommentAdp = new CommentAdp(R.layout.adp_comment, mComments, width);
//        mCommentAdp.openLoadAnimation(new CustomAnimation());
        mCommentAdp.setNotDoAnimationCount(3);
        mCommentAdp.isFirstOnly(false);
        mRecycleView.setAdapter(mCommentAdp);
    }

    @Override
    public void initData() {
        loadData(!hasDataInPage(), false);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
        mCommentAdp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CommentModel model = (CommentModel) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.id_comment_rela:
                        //回复
                        Intent intent = new Intent(aty, ReplyCommentAty.class);
                        intent.putExtra(IntentKey.General.KEY_MODEL, model);
                        showActivity(aty, intent);
                        break;
                    case R.id.id_comment_del_txt:
                        //删除评论
                        mRecordItemPos = position;
                        mRecordItemId = model.uid;
                        popupView.showAtLocPop(mRootViewLL, POPUP_ITEMS);
                        break;
                }
            }
        });
    }

    @Override
    public void OnItemClick(int position, String tag) {
        //删除评论或者回复
        ((CommentPresenter) presenter).delMyComment(true, mRecordItemId);
    }

    /**
     * 加载数据
     *
     * @param loadMore 是否加载更多
     */
    public void loadData(boolean isShowLoad, boolean loadMore) {
        ((CommentPresenter) presenter).getComments(loadMore, isShowLoad, "5", mUid);
        if (!loadMore) {
            stopRefresh(mXRefreshView, 2000);
        } else {
            stopLoadMore(mXRefreshView, 2000);
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.DEL_COMMENT_SUCCESS)) {
                //删除评论成功
                if (mComments.size() == 1) {
                    mComments.clear();
                    dataStatus(3);
                } else {
                    mComments.remove(mRecordItemPos);
                    mCommentAdp.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.COMMENT_LIST_SUCCESS)) {
                if (!loadMore) {
                    mComments.clear();
                    mXRefreshView.stopRefresh();
                } else {
                    mXRefreshView.stopLoadMore();
                }
                //柴油批发信息
                List<CommentModel> comments = (List<CommentModel>) t;
                if (!comments.isEmpty()) {
                    mComments.addAll(comments);
                    mCommentAdp.notifyDataSetChanged();
                } else {
                    if (loadMore) {
//                        mXRefreshView.setLoadComplete(true);
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //没有数据
                    }
                }
            }
        }
    }
}
